package servlets;



import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.AdministradorEmpleado;
import manager.AdministradorLocal;

public class LoginServer extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String jspPage;
		
		String user = req.getParameter("user");
		String pass = req.getParameter("password");
		boolean aut = false;
       	aut = AdministradorEmpleado.getInstancia().autoUsu(user, pass);
       	
       	 if (aut == false)
       	 {

       		
     		jspPage = "/jsp/invalidLogin.jsp";
       		req.setAttribute("mensaje", "Todo Bien");
       		dispatch(jspPage, req, resp);
       		
       	 }
       	 else
       	 {
        		String localid = String.valueOf(AdministradorLocal.getInstancia().getLocalId(user, pass));
       			Date now = new Date();
       			String timestamp = now.toString();
       			Cookie cookie = new Cookie("userLocal", localid);
       			cookie.setMaxAge(365 * 24 * 60 * 60);
       			cookie.setComment("Seteamos La cookie del local yupiiii");
       			resp.addCookie(cookie);
           		
        		jspPage = "/index.html";
           		req.setAttribute("mensaje", "Todo Bien");
           		dispatch(jspPage, req, resp);
       	 }
	}

	 protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
         if (jsp != null)
         {
             RequestDispatcher rd = request.getRequestDispatcher(jsp);
             rd.forward(request, response);
         }
     }
}